package cn.com.lms.business.exam.service;

import cn.com.lms.app.api.index.vo.ToDisposeVO;
import cn.com.lms.app.api.exam.vo.UserExerciseVO;
import cn.com.lms.business.core.constant.ErrorEnum;

import cn.com.lms.business.exam.persistence.entity.*;

import cn.com.lms.business.exam.persistence.repository.ExerciseDepartJobInfoRepository;
import cn.com.lms.business.exam.persistence.repository.ExerciseRepository;
import cn.com.lms.business.exam.web.vo.DepartJobInfoVO;


import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.persistence.query.QueryHelpBean;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

@Service
public class ExerciseService extends BaseService<ExerciseRepository, Exercise, Long> {



    @Autowired
    ExerciseQuestionService exerciseQuestionService;

    @Autowired
    ExerciseEmployeeService exerciseEmployeeService;

    @Autowired
    ExerciseDepartJobInfoService exerciseDepartJobInfoService;


    public static QueryHelpBean getQueryHelpBean() {
        String selectSQL = "select ee.*, (select count(0) from bnz_exam_question_subject qs where qs.is_delete=0 and qs.question_id in(select eq.question_id from bnz_exam_exercise_question eq where eq.exercise_id=ee.id )) subject_count ," +
                " (select count(1) from bnz_exam_exercise_employee em where em.is_delete=0 and em.exercise_id=ee.id) employee_count, " +
                " (select group_concat(CONCAT(q.question_name)) from bnz_exam_exercise_question exq  LEFT JOIN bnz_exam_question q  on exq.question_id=q.id   where exq.exercise_id=ee.id " +
                " GROUP BY exq.exercise_id) question_names ";
        String whereSQL = " from bnz_exam_exercise ee where 1=1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, ExerciseVO.class);
        return queryBean;
    }

    public static QueryHelpBean getUserExerciseQueryHelpBean() {
        String selectSQL = "select em.employee_id user_id,em.exercise_id as id, ex.exercise_name as name ,ex.create_time, " +
                " (select count(DISTINCT(es.subject_id)) from bnz_exam_employee_exercise_subject es where es.employee_id=em.employee_id and  es.exercise_id=em.exercise_id and es.subject_id in (select sub.id from bnz_exam_question_subject sub where sub.question_id in(select question_id from bnz_exam_exercise_question eq where eq.exercise_id=em.exercise_id and sub.is_delete=0) ) ) finish_count," +
                " (select count(1) from bnz_exam_question_subject sub where sub.question_id in(select question_id from bnz_exam_exercise_question eq where eq.exercise_id=em.exercise_id and sub.is_delete=0)) subject_count , " +
                " (select count(1) from bnz_exam_exercise_question eq where eq.exercise_id=em.exercise_id) question_count";
        String whereSQL = " from bnz_exam_exercise_employee em LEFT JOIN bnz_exam_exercise ex on em.exercise_id=ex.id where  ex.id is not null  ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, UserExerciseVO.class);
        return queryBean;
    }

    @Transactional(rollbackFor = Exception.class)
    public Exercise addExercise(Exercise exercise) {
        // 保存试卷信息
        Exercise result = save(exercise);
        //选择题库
        exerciseQuestionService.batchSave(result, exercise.getExerciseQuestionList());

        // 部门，岗位信息
        exerciseDepartJobInfoService.batchSave(result,exercise.getDepartJobInfo());

        //选择人员
        List<ExerciseEmployee> exerciseEmployeeList=exercise.getExerciseEmployeeList();
        exerciseEmployeeService.batchSave(result,exerciseEmployeeList);

        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public Exercise updateExercise(Exercise target) {

        save(target);

        List<ExerciseQuestion> exerciseQuestionList = target.getExerciseQuestionList();
        exerciseQuestionService.deleteByExerciseId(target.getId());
        exerciseQuestionService.batchSave(target, exerciseQuestionList);

        List<ExerciseEmployee> exerciseEmployeeList = target.getExerciseEmployeeList();
        exerciseEmployeeService.deleteByExerciseId(target.getId());
        exerciseEmployeeService.batchSave(target, exerciseEmployeeList);

        // 部门，岗位信息
        List<DepartJobInfoVO> departJobInfo=target.getDepartJobInfo();
        exerciseDepartJobInfoService.deleteByExerciseId(target.getId());
        exerciseDepartJobInfoService.batchSave(target,departJobInfo);


        return target;
    }


    public int updateIsDelete(Boolean isDelete, List<Long> id) {

        return repository.updateIsDelete(isDelete, id);
    }

    public int updateIsEnable(Boolean isEnable, List<Long> id) {
        return repository.updateIsEnable(isEnable, id);
    }

    /**
     * 待办事项
     */
    public static QueryHelpBean getToDisposeQueryHelpBean(Long userId) {
        String selectSQL = "select tem.* ";
        String whereSQL = "from (" +
                " SELECT   em.exercise_id AS id,ex.exercise_name AS name,'' AS pictureUrl,2  AS type,null as endTime,ex.create_time AS dataTime," +
                " (select count(1) from bnz_exam_exercise_question  eq where eq.exercise_id=em.exercise_id) questionCount," +
                " (select count(1) from bnz_exam_employee_exercise_subject es where es.exercise_id=em.exercise_id) finishCount," +
                "  (select count(1) from bnz_exam_question_subject sub where sub.question_id in(select question_id from bnz_exam_exercise_question eq where eq.exercise_id=em.exercise_id and sub.is_delete=0)) subjectCount,0 as sumHours,0 as learningHours,0 score" +
                " FROM bnz_exam_exercise_employee em LEFT JOIN bnz_exam_exercise ex ON em.exercise_id = ex.id " +
                " WHERE ex.is_enable=1 and ex.is_delete=0 and ex.id IS NOT NULL and em.employee_id="+userId+" UNION ALL" +
                " SELECT lp.id,lp.plan_name AS name,lp.cover_url as pictureUrl,1  AS type,lp.finish_time AS endTime,lp.release_time dataTime, 0 as questionCount,0 finishCount,0 subjectCount," +
                "\t(SELECT\tsum( ci.course_time ) FROM bnz_learning_stage_detail a\tLEFT JOIN bnz_course_info ci ON a.obj_id = ci.id WHERE a.study_type = 0   AND a.plan_id=lp.id ) AS sumHours,\n" +
                "\tsum( lsd.learning_hours ) AS learningHours,\n" +
                "\tlp.score AS score " +
                " FROM bnz_learning_student lsd LEFT JOIN bnz_learning_plan lp ON lsd.plan_id = lp.id " +
                " WHERE lsd.user_id = " + userId + " and lp.is_delete=0 and lsd.finish_state=0 GROUP BY lsd.plan_id " +
                " HAVING (select count(ls.id) from bnz_learning_student ls where ls.user_id=user_id and ls.plan_id=lsd.plan_id and ls.finish_state=1) !=\n" +
                " (select count(ls.id) from bnz_learning_student ls where ls.user_id=user_id and ls.plan_id=lsd.plan_id ) ) tem  where 1=1";

        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, ToDisposeVO.class);
        return queryBean;
    }


}
